package com.huadao.cart_ccs.experiment.injection.addViruses;

import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.ExperimentStageManager;
import com.huadao.cart_ccs.common.ProjectManager;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.common.dialog.DialogFragmentHelper;
import com.huadao.cart_ccs.common.dialog.IDialogResultListener;
import com.huadao.cart_ccs.experiment.injection.InjectionContract;
import com.huadao.cart_ccs.experiment.injection.InjectionPresenter;
import com.huadao.cart_ccs.experiment.injection.OnInject;
import com.huadao.cart_ccs.greendao.entity.Injection;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AddVirusesDF extends BaseDialogFragment implements InjectionContract.IInjectionView {

    @BindView(R.id.btn_startInject)
    Button btn_startInject;
    @BindView(R.id.btn_finishInject)
    Button btn_finishInject;
    @BindView(R.id.btn_save)
    Button btn_save;
    @BindView(R.id.sp_inject_type)
    Spinner spInjectType;
    @BindView(R.id.et_cellVolume)
    EditText etCellVolume;
    @BindView(R.id.et_cellAmount)
    EditText etCellAmount;
    @BindView(R.id.et_TransAct)
    EditText etTransAct;
    @BindView(R.id.et_totalVolume)
    EditText etTotalVolume;
    @BindView(R.id.switch_isMixed)
    Switch switch_isMixed;
    @BindView(R.id.tv_operate_date)
    TextView tvOperateDate;
    @BindView(R.id.tv_operate_time)
    TextView tvOperateTime;
    @BindView(R.id.tv_inject_people)
    TextView tvInjectPeople;
    @BindView(R.id.tv_history_record)
    TextView tvHistoryRecord;

    private OnInject onInject;

    private Date startDate;
    private Date finishDate;

    private InjectionPresenter presenter;
    private Injection injection;

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected boolean isDialog() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_injection;
    }

    @Override
    protected void onInit() {
        this.setCancelable(false);
        presenter = new InjectionPresenter(this);
        Date date = new Date();
        tvOperateDate.setText(TimeUtil.date_sdf.format(date));
        tvOperateTime.setText(TimeUtil.time_sdf.format(date));
//        presenter.getHistoryInjection();
    }

    @Override
    public void onGetHistoryInjectionList(List<Injection> injectionList) {
        if (injectionList == null || injectionList.size() == 0) {
            tvHistoryRecord.setVisibility(View.GONE);
            return;
        }
        for (Injection injection : injectionList) {
            String text = String.format("登陆者于%tF %tT注入了总体积为%dml的细胞悬液（细胞体积%dml，细胞量%d个，TransAct %dml）", injection.getStartDate(), injection.getStartDate(), injection.getTotalVolume(), injection.getCellVolume(), injection.getCellAmount(), injection.getTransAct());
            tvHistoryRecord.setVisibility(View.VISIBLE);
            tvHistoryRecord.append(text);
        }
    }

    public void setOnInject(OnInject onInject) {
        this.onInject = onInject;
    }

    @OnClick({R.id.btn_close, R.id.btn_startInject, R.id.btn_finishInject, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                this.dismiss();
                break;
            case R.id.btn_startInject:
                String err = checkParams();
                if (err != null) {
                    showToast(err);
                    return;
                }
                injection = generateInjectionData();

                DialogFragmentHelper.showConfirmDialog(getActivity(), "确认添加", "请确认细胞悬液袋的卡纸是否开启！系统将开启进液泵，进行添加细胞！", new IDialogResultListener<Integer>() {
                    @Override
                    public void onDataResult(Integer result) {
                        if (result == DialogInterface.BUTTON_POSITIVE) {
                            startInject();
                        }
                    }
                }, true);
                break;
            case R.id.btn_finishInject:
                DialogFragmentHelper.showConfirmDialog(getActivity(), "确认冲刷", "完成添加细胞进入到冲刷阶段，请确认细胞悬液袋的卡纸是否关闭！系统将开启进液泵，用培养液进行冲刷！", new IDialogResultListener<Integer>() {
                    @Override
                    public void onDataResult(Integer result) {
                        if (result == DialogInterface.BUTTON_POSITIVE) {
                            finishDate = new Date();
                            injection.setFinishDate(finishDate);
                            if (onInject != null) {
                                onInject.sour();
                            }
                        }
                    }
                }, true);
//                finishInject();
//                btn_finishInject.setVisibility(View.INVISIBLE);
//                btn_save.setVisibility(View.VISIBLE);

                break;
//            case R.id.btn_save:
//                saveInject();
//                this.dismiss();
//                break;
        }
    }

    private void startInject() {
        startDate = new Date();
        injection.setStartDate(startDate);
        if (onInject != null) {
            onInject.injectStart(injection);
        }
    }

    public void startInjectionOK() {
        btn_startInject.setText("正在添加细胞...");
        btn_startInject.setClickable(false);
//        btn_startInject.setVisibility(View.GONE);
//        btn_finishInject.setVisibility(View.VISIBLE);
    }

    public void finishInject() {
        btn_startInject.setVisibility(View.GONE);
        btn_finishInject.setVisibility(View.VISIBLE);
    }

    //冲刷完成
    public void finishSour() {
        DialogFragmentHelper.showMessageDialog(getActivity(), "冲刷", "冲刷完成");
        presenter.saveInjection(injection);
//        presenter.getHistoryInjection();
        btn_finishInject.setVisibility(View.GONE);
//        btn_startInject.setVisibility(View.GONE);
//        btn_finishInject.setVisibility(View.VISIBLE);
//        if (onInject != null) {
//            onInject.saveInjectData(injection);
//        }
    }

//    private void saveInject() {
//        String err = checkParams();
//        if (err != null) {
//            showToast(err);
//            return;
//        }
//        if (onInject != null) {
//            onInject.saveInjectData(injection);
//        }
//    }

    private String checkParams() {
        String err = null;
        if (TextUtils.isEmpty(spInjectType.getSelectedItem().toString())) {
            err = "请输入进样类型";
        } else if (TextUtils.isEmpty(etCellVolume.getText())) {
            err = "请输入体积";
        } else if (TextUtils.isEmpty(etCellAmount.getText())) {
            err = "请输入细胞量";
        } else if (TextUtils.isEmpty(etTransAct.getText())) {
            err = "请输入TransAct";
        } else if (TextUtils.isEmpty(etTotalVolume.getText())) {
            err = "请输入总体积";
        }
//        else if (startDate == null) {
//            err = "请输入操作开始时间";
//        } else if (finishDate == null) {
//            err = "请输入操作完成时间";
//        }
        return err;
    }

    private Injection generateInjectionData() {
        Injection injection = new Injection();
        injection.setInjectionId(null);
        injection.setProjectId(ProjectManager.getInstance().getProjectId());
        injection.setStageId(ExperimentStageManager.getInstance().getStageId());
        injection.setType(spInjectType.getSelectedItem().toString());
        injection.setCellVolume(Integer.parseInt(etCellVolume.getText().toString()));
        injection.setCellAmount(Integer.parseInt(etCellAmount.getText().toString()));
        injection.setTransAct(Integer.parseInt(etTransAct.getText().toString()));
        injection.setTotalVolume(Integer.parseInt(etTotalVolume.getText().toString()));
        injection.setMixed(switch_isMixed.isChecked());
        injection.setStartDate(startDate);
        injection.setFinishDate(finishDate);
        injection.setOperator(tvInjectPeople.getText().toString());
        return injection;
    }
}